package com.crazyktv.wcf.android;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class Control extends Activity {
	protected static Vibrator myVibrator = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_control);
		findViews();
		myVibrator = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
	}

	private ImageButton btnReset, btnMale, btnFemale, btnKeyDn, btnKeyOri,
			btnKeyUp, btnPlayBa, btnPlay, btnPlayFw, btnVolDn, btnMute,
			btnVolUp;

	private void findViews() {
		btnReset = (ImageButton) findViewById(R.id.button_reset);
		btnMale = (ImageButton) findViewById(R.id.button_male);
		btnFemale = (ImageButton) findViewById(R.id.button_female);
		btnKeyOri = (ImageButton) findViewById(R.id.button_keyori);
		btnKeyDn = (ImageButton) findViewById(R.id.button_keydn);
		btnKeyUp = (ImageButton) findViewById(R.id.button_keyup);
		btnPlayBa = (ImageButton) findViewById(R.id.button_playba);
		btnPlay = (ImageButton) findViewById(R.id.button_play);
		btnPlayFw = (ImageButton) findViewById(R.id.button_playfw);
		btnVolDn = (ImageButton) findViewById(R.id.button_voldn);
		btnMute = (ImageButton) findViewById(R.id.button_mute);
		btnVolUp = (ImageButton) findViewById(R.id.button_volup);
		btnReset.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoAction(Util.DoAction.RESET, "");
				myVibrator.vibrate(100);
			}
		});
		btnMale.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.KEY_MALE, "");
				myVibrator.vibrate(100);
			}
		});
		btnFemale.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.KEY_FEMALE, "");
				myVibrator.vibrate(100);
			}
		});
		btnKeyOri.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.KEY_ORI, "");
				myVibrator.vibrate(100);
			}
		});
		btnKeyDn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.KEY, "-1");
				myVibrator.vibrate(100);
			}
		});

		btnKeyUp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.KEY, "1");
				myVibrator.vibrate(100);
			}
		});
		btnPlayBa.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoAction(Util.DoAction.PALY_BA, "");
				myVibrator.vibrate(100);
			}
		});
		btnPlay.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoAction(Util.DoAction.PLAY, "");
				myVibrator.vibrate(100);
			}
		});
		btnPlayFw.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoAction(Util.DoAction.PLAY_FW, "");
				myVibrator.vibrate(100);
			}
		});
		btnVolDn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.VOL, "-1");
				myVibrator.vibrate(100);
			}
		});
		btnMute.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.MUTE, "");
				myVibrator.vibrate(100);
			}
		});
		btnVolUp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(Util.DoControl.VOL, "1");
				myVibrator.vibrate(100);
			}
		});
	}

	// listen "back" action
	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), R.string.quit,
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
